﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;
using System;

/// <summary>
/// 阿里云 视频点播
/// 
/// 注意: 挂载该脚本的物体必须命名为:  AliyunVodHelper
/// </summary>
public class AliyunVodHelper : MonoBehaviour
{
    /// <summary>
    /// STS 方式上传视频&Cover图片
    /// </summary>
    /// <param name="user">User.</param>
    /// <param name="title">Title.</param>
    /// <param name="desc">Desc.</param>
    /// <param name="cateId">Cate identifier.</param>
    /// <param name="tags">Tags.</param>
    /// <param name="videoPath">Video path.</param>
    /// <param name="imagePath">Image path.</param>
    public void PushVideoBySTS(string user, string title, string desc, string cateId, string tags, string videoPath, string imagePath, Action<string> pushVideoSucess)
    {
        curUserId = user;
        curTitle = title;
        curDesc = desc;
        curCateId = cateId;
        curTags = tags;
        curVideoPath = videoPath;
        curImagePath = imagePath;
        PushFileSucessCallback = pushVideoSucess;

        STSUpLoadFile();
    }

    void STSUpLoadFile()
    {
        BestHttpUtil.Instance.SendHTTPRequestWithCallBack(stsUrl + "user_name=" + curUserId, (string dataStr) =>
        {
            Debug.Log("GGL_  服务器返回的数据: dataStr= " + dataStr);

            Response_General<OSSResModel> response = JsonUtility.FromJson<Response_General<OSSResModel>>(dataStr);
            Debug.Log("GGL_  Unity  获取视频上传 Auth  --------------------- \n");
            Debug.Log("accessKeyId= " + response.body.AccessKeyId);
            Debug.Log("accessKeySecret= " + response.body.AccessKeySecret);
            Debug.Log("securityToken= " + response.body.SecurityToken);

            vodPushVideo(
                response.body.AccessKeyId,
                response.body.AccessKeySecret,
                response.body.SecurityToken,
                curTitle,
                curDesc,
                curCateId,
                curTags,
                curVideoPath,
                curImagePath
            );

        });
    }



    //回调方法------------------
    public void PushSucessCallBack(string str)
    {
        Debug.Log("Unity 上传成功的回调方法 ! \n");

        Debug.Log("Unity 参数: str1= " + str + "\n");

        string[] temp = str.Split('_');
        string videoId = temp[0];
        string imageUrl = temp[1];
        Debug.Log("Unity videoId= " + videoId + ";  imageUrl= " + imageUrl);

        Dictionary<string, string> posFields = new Dictionary<string, string>
        {
            {"user_name", curUserId},
            {"title", curTitle},
            {"sub_title", "默认子标题"},
            {"keyword", "default"},
            {"category_1", "0"},
            {"category_2", "0"},

            {"category_3", "0"},
            {"completeState", "1"},
            {"isserial", "0"},
            {"author_name", "作者"},
            {"booksynopsis", "测试测试"},

            {"cover", imageUrl},
            {"tags", "默认"},
            {"is_public", "1"},
            {"chapter_fanwai", "0"},
            {"is_chapter", "1"},
            {"chapter_url", videoId}
        };
        string backUrl = @"https://api.cavefire.com/vod/create";
        BestHttpUtil.Instance.SendHTTPPostRequestWithCallBack(backUrl, posFields, (string dataStr) =>
        {
            Debug.Log("Unity 视频发送成功, 通知应用服务器: dataStr= " + dataStr);

            //Response_General<OSSResModel> response = JsonUtility.FromJson<Response_General<OSSResModel>>(dataStr);

        });

        PushFileSucessCallback(str);
    }

    //底层方法--------------------------------------------
    const string stsUrl = @"https://api.cavefire.com/sts/vod/sts?";

    public static AliyunVodHelper Instance;

    public Action<string> PushFileSucessCallback;

    //上层接口--------------------------------------------
    string curUserId;
    string curTitle;
    string curDesc;
    string curCateId;
    string curTags;

    string curVideoPath;
    string curImagePath;

    // Start is called before the first frame update
    void Awake()
    {
        Instance = this;
    }


    [DllImport("__Internal")]
    private static extern void vodPushVideo(
        string accessKeyId,
        string accessKeySecret,
        string accessKeyToken,

        string title, 
        string desc,
        string cateId,
        string tags,
        string videoPath,
        string imagePath
    );


}
